﻿(function ($) {
    var modaldialog = {};

    var minWidth = 300, minHeight = 120, minTop = 25, fixedDialogTop = 150;
    var dialog, dialogmask;

    function createDialog(content, title, resizeCallBack, closeCallBack) {

        if ($('#dialog').length != 0) $('#dialog').remove();
        if ($('#dialog-mask').length != 0) $('#dialog-mask').remove();

        dialog = $("<div id='dialog'>" +
                            "<div id='dialog-header'>" +
	                            "<div id='dialog-title'></div>" +
                                "<div id='dialog-close'></div>" +
                            "</div>" +
                        "</div>");

        dialog.css("visibility", "hidden");

        dialogmask = $("<div id='dialog-mask'></div>");
        dialogmask.hide();
        setMaskStyles();

        dialog.append(content);
        $("form").append(dialog);
        $("form").append(dialogmask);

        if (resizeCallBack != null) resizeCallBack(content);

        var dialogTitle = $('#dialog-title');
        dialogTitle.html(title);

        var closeButton = $('#dialog-close');
        closeButton.click(function () { closeCallBack(content); });
    }

    function setMaskStyles() {
        if (dialogmask == null || dialogmask == undefined || dialogmask.length == 0) return;

        dialogmask.css({
            width: getWindowWidth(window),
            height: getWindowHeight(window),
            filter: "alpha(opacity=40)"
        });
    }

    function showDialog() {
        dialogmask.fadeIn("normal");
        dialog.fadeIn("slow", function () { dialog.css("visibility", "visible") });
    }

    function resize(content) {
        var titleHeight = $('#dialog-header').height();
        var contentHeight = content.height();
        var contentWidth = content.width();
        var dialogTop = Math.abs($(window).height() - contentHeight - titleHeight) / 2;

        dialog.css({
            width: Math.max(minWidth, contentWidth),
            height: Math.max(minHeight, contentHeight) + titleHeight
        });
        dialog.css('left', ($(window).width() - contentWidth) / 2);
        dialog.css('top', fixedDialogTop);
    }

    function setDialogSize() {
        var frame = $("#dialog_container");

        var titleHeight = $('#dialog-header').height();
        var width, height;

        if (frame.length > 0) {
            width = frame[0].contentWindow.document.body.clientWidth;
            height = frame[0].contentWindow.document.body.clientHeight;
            frame.css({
                width: Math.max(minWidth, width),
                height: Math.max(minHeight, height)
            });
            dialog.css({
                width: Math.max(minWidth, width),
                height: Math.max(minHeight, height) + titleHeight
            });
        }
        var windowWidth = getWindowWidth(window);
        var windowHeight = getWindowHeight(window);

        var dialogTop = Math.abs(windowHeight - height - titleHeight) / 2;
        dialog.css('left', (windowWidth - width) / 2);
        dialog.css('top', "50px");
    }

    function isChildWindow() {
        return $(window.parent.document).find("iframe").length > 0;
    }

    function getWindowWidth(targetWindow) {
        var documentElement = targetWindow.document.documentElement;
        return Math.max(documentElement.scrollWidth, targetWindow.document.body.clientWidth);
    }

    function getWindowHeight(targetWindow) {
        var documentElement = targetWindow.document.documentElement;
        return Math.max(documentElement.scrollHeight, documentElement.clientHeight);
    }

    function attachResizeEventHandler(resizeHandler) {
        $(window).unbind("resize.modalDialogResize");
        $(window).bind("resize.modalDialogResize", function () {
            setMaskStyles();
            resizeHandler();
        })
    }

    modaldialog.changeTitle = function $$modaldialog$changeTitle(title) {
        var dialogTitle = $('#dialog-title');
        dialogTitle.html(title);
    }

    modaldialog.showNewPage = function $$modaldialog$showNewPage(url, title) {
        var frame = $("<iframe id='dialog_container' src='about:blank' frameborder='0' scrolling='no' style='width:100%;' />");
        frame.attr("src", url);
        frame.load(function () {
            showDialog();
            setDialogSize();
        });
        createDialog(frame, title, null, modaldialog.remove);

        attachResizeEventHandler(setDialogSize);
    }
    modaldialog.show = function $$modaldialog$show(content, title) {
        modaldialog.showDialogWithCallback(content, title, null);
    }
    modaldialog.showDialogWithCallback = function $$modaldialog$showDialogWithCallback(content, title, callBack) {
        closeCallback = function () {
            if (callBack != null) callBack();
            modaldialog.hide(content);
        }
        createDialog(content.show(), title, resize, closeCallback);
        showDialog();
        if (isChildWindow()) hideDialogHeader();

        attachResizeEventHandler(function () { resize(content); });
    }
    modaldialog.remove = function $$modaldialog$remove() {
        $(window).unbind("resize.modalDialogResize");
        dialog.fadeOut("slow", function () { $(this).remove(0); });
        dialogmask.fadeOut("normal", function () { $(this).remove(0); });
    };
    modaldialog.hide = function $$modaldialog$hide(content) {
        $("form").append(content.hide());
        modaldialog.remove();
        if (isChildWindow()) showDilaogHeader();
    };
    function hideDialogHeader() {
        var parentBody = $(window.parent.document).find("body");
        var header = $("#dialog-header", parentBody);
        var headerMask = $("<div id='header-mask' class='HeaderMask'></div>");

        headerMask.hide();
        parentBody.append(headerMask);

        headerMask.css({
            top: header.offset().top,
            left: header.offset().left,
            filter: "alpha(opacity=40)"
        })
        headerMask.fadeIn("normal");
    }
    function showDilaogHeader() {
        var headerMask = $(window.parent.document).find(".HeaderMask");
        headerMask.remove();
    }
    $.extend({ modaldialog: modaldialog });
})(jQuery);

function changeDialogTitle(title) {
    $.modaldialog.changeTitle(title);
}

function closeModalDialog() {
    closeModalDialog(null);
}

function closeModalDialog(callBack) {
    $.modaldialog.remove();
    if (callBack != null) callBack();
}

function hideModalDialog(content) {
    $.modaldialog.hide(content);
}

function setModalContext(context) {
    $("#" + ModalDialogContextControID).val(context);
}